package net.zzy.pbbot.plugin.callMeStudy.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotContainer;
import net.zzy.pbbot.plugin.callMeStudy.entity.CallMeDaoEntity;
import net.zzy.pbbot.plugin.callMeStudy.mapper.CallMeMapper;
import net.zzy.pbbot.plugin.todo.entity.QqDaoEntity;
import net.zzy.pbbot.plugin.todo.mapper.QqMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static net.zzy.pbbot.finalfield.FinalInt.BOT_QQ;

/**
 * @author zhuZhaoYang
 * @date 2021/9/29 20:00
 */
@Component
@Slf4j
public class CallMeToStudy {

    private BotContainer botContainer;

    @Autowired
    public void setBotContainer(BotContainer botContainer) {
        this.botContainer = botContainer;
    }

    private CallMeMapper callMeMapper;

    @Autowired
    public void setCallMeMapper(CallMeMapper callMeMapper) {
        this.callMeMapper = callMeMapper;
    }

    private QqMapper qqMapper;

    @Autowired
    public void setQqMapper(QqMapper qqMapper) {
        this.qqMapper = qqMapper;
    }


    /**
     * 每天10,14,16,19,21时
     */
    @Scheduled(cron = "0 0 10,14,16,19,21,22 * * ?")
    public void sendMsgToCallMe() {
        String call = "。如果你无所事事，那就学习吧。";
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("HH:mm");
        String time = format.format(date);
        Bot bot = botContainer.getBots().get(BOT_QQ);
        if (bot == null) {
            log.error("Bot为空,BotQQ号为:{},请确认当前登录Bot是否正确。", BOT_QQ);
            return;
        }
        List<CallMeDaoEntity> callMeDaoEntities = callMeMapper.selectList(null);
        for (CallMeDaoEntity callMeDaoEntity : callMeDaoEntities) {
            QqDaoEntity number = qqMapper.selectOne(new QueryWrapper<QqDaoEntity>().eq("id", callMeDaoEntity.getQqId()));
            bot.sendPrivateMsg(number.getNumber(), "现在是" + time + call, false);
        }
    }
}
